<?php
!defined('IN_ROOT') && exit( 'Access Denied' );
class GiftsController extends AdminModuleController {	

	function _category($model=0) {
		$categorys = $this->CategoryModel->findAll("`storeId`=1 AND parentId='0'");
		$r = array();
		foreach($categorys as $v) {
			$r[] = $v;			
			$arr = $this->CategoryModel->findAll("parentId='{$v['categoryId']}'");
			foreach($arr as &$v2) {
				$v2['cateName'] = "&nbsp;&nbsp;&nbsp;&nbsp;--".$v2['cateName'];
			}
			$r = array_merge($r, $arr);
		}
		return $r;
	}
	
	public function actionAdmin() {
		$page = max(1, getgpc('page'));
		
		$sqladd = "";
		$title = getgpc('title', 'R');
		if($title) {
			$sqladd = "title like '%$title%'";
		}
		
		$giftsList = $this->GiftsModel->findAll($sqladd, $page, GODHOUSE_PPP3);
		foreach($giftsList as &$v) {
			$category = $this->CategoryModel->findByPk($v['categoryId']);
			$v['categoryName'] = $category['cateName'];
			$v['createdTime2'] = date('Y-m-d', $v['createdTime']);
			$v['date'] = date('Y-m-d', $v['startDate']).'/'.date('Y-m-d', $v['endDate']);;
		}
		
		$num = $this->GiftsModel->count($sqladd);
		$multipage = page($num, GODHOUSE_PPP3, $page, 'gifts-admin.htm');
		
		$this->view->assign('title', $title);
		$this->view->assign('multipage', $multipage);
		$this->view->assign('giftsList', $giftsList);
		$this->display('gifts_admin');
	}
	
	public function actionCreate() {
		if($gifts = getgpc('gifts', 'P')) {
			$gifts['createdTime'] = $this->time;
			
			if($_FILES['picture']['name']) {
				$upload = new Upload();
				$upfile = $upload->upfile($_FILES['picture'], 'upfile/gifts');
				$gifts['picture'] = $upfile;
			}
			
			$gifts['startDate'] = strtotime($gifts['startDate']);
			$gifts['endDate'] = strtotime($gifts['endDate']);
			$gifts['createdUser'] = $this->userId;
			
			$this->GiftsModel->insert($gifts);
			/*
			if($this->GiftsModel->insert($gifts)) {
				$giftsCount = $this->GiftsModel->count("categoryId='{$gifts['categoryId']}'");				
				$this->CategoryModel->update("categoryId='{$gifts['categoryId']}'",array('giftsCount' => $giftsCount));
			}*/
			
			$this->message('添加成功。', 'gifts-admin.htm');
		}

		$this->view->assign('categorys', $this->CategorysPublic->get_options(GODHOUSE_CATEGORY_GIFTS));
		$this->view->assign('action', 'create');
		$this->display('gifts_create');
	}
	
	public function actionUpdate() {
		$giftsId = getgpc(2);
		$model = $this->GiftsModel->findByPk($giftsId);
		if($gifts = getgpc('gifts', 'P')) {
			
			if($_FILES['picture']['name']) {
				if($model['picture']) {
					@unlink(GODHOUSE_ROOT.substr($model['picture'], 1));
				}
				
				$upload = new Upload();
				$upfile = $upload->upfile($_FILES['picture'], 'upfile/gifts');
				$gifts['picture'] = $upfile;
			}
			
			$gifts['startDate'] = strtotime($gifts['startDate']);
			$gifts['endDate'] = strtotime($gifts['endDate']);
			
			$this->GiftsModel->updateByPk($giftsId, $gifts);
			
			/*	
			if($gifts['categoryId']!=$model['categoryId'])
			{
				$giftsCount = $this->GiftsModel->count("categoryId='{$model['categoryId']}'");		
				$this->CategoryModel->update("categoryId='{$model['categoryId']}'",array('giftsCount' => $giftsCount));
				
				$giftsCount = $this->GiftsModel->count("categoryId='{$gifts['categoryId']}'");		
				$this->CategoryModel->update("categoryId='{$gifts['categoryId']}'",array('giftsCount' => $giftsCount));
			}*/
			$this->alert('修改成功。', 'gifts-admin.htm');
		}
		
		$model['startDate'] = date('Y-m-d', $model['startDate']);
		$model['endDate'] = date('Y-m-d', $model['endDate']);

		$this->view->assign('categorys', $this->CategorysPublic->get_options(GODHOUSE_CATEGORY_GIFTS));
		$this->view->assign('gifts', $model);
		$this->view->assign('giftsId', $giftsId);
		$this->view->assign('action','update');
		$this->display('gifts_create');
	}
	
	public function actionDelete() {
		$giftsId = getgpc(2);
		
		$gifts = $this->GiftsModel->findByPk($giftsId);
		if(!$gifts) {
			$this->message('礼品不存在。', GODHOUSE_DOMAIN_WWW.'admin/gifts-admin.htm');
		}
		$this->GiftsModel->deleteByPk($giftsId);
		$this->message('删除成功。', GODHOUSE_DOMAIN_WWW.'admin/gifts-admin.htm');
	}
}